package com.quectel.core.module.scheduling.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.core.scheduling.SchedulingAuditPipelineSeqConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.scheduling.dao.SchedulingAuditPipelineSeqDao;
import com.quectel.core.module.scheduling.dto.SchedulingAuditPipelineDto;
import com.quectel.core.module.scheduling.dto.SchedulingAuditPipelineSeqDeptPersonDto;
import com.quectel.core.module.scheduling.dto.SchedulingAuditPipelineSeqDto;
import com.quectel.core.module.scheduling.entity.SchedulingAuditPipelineSeqEntity;
import com.quectel.core.module.scheduling.service.SchedulingAuditPipelineSeqDeptPersonService;
import com.quectel.core.module.scheduling.service.SchedulingAuditPipelineSeqService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/05 13:24
 */
@DubboService
public class SchedulingAuditPipelineSeqServiceImpl implements SchedulingAuditPipelineSeqService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingAuditPipelineSeqServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_AUDIT_PIPELINE_SEQ_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_AUDIT_PIPELINE_SEQ_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingAuditPipelineSeqDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_AUDIT_PIPELINE_SEQ_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingAuditPipelineSeqDao schedulingAuditPipelineSeqDao;
    @Autowired
    private SchedulingAuditPipelineSeqDeptPersonService schedulingAuditPipelineSeqDeptPersonService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearRelationRecordByAuditPipelineId(Long auditPipelineId) {
        List<SchedulingAuditPipelineSeqEntity> entities = schedulingAuditPipelineSeqDao.selectList(
                new LambdaQueryWrapper<SchedulingAuditPipelineSeqEntity>().
                        eq(SchedulingAuditPipelineSeqEntity::getAuditPipelineId, auditPipelineId));
        for (SchedulingAuditPipelineSeqEntity entity : entities) {
            //删除序列下的人
            schedulingAuditPipelineSeqDeptPersonService.deleteByAuditPipelineSeqId(entity.getId());
            //删除单个序列
            schedulingAuditPipelineSeqDao.deleteById(entity.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(SchedulingAuditPipelineDto auditPipelineDto) {

        clearRelationRecordByAuditPipelineId(auditPipelineDto.getId());


        if (auditPipelineDto.getAuditPipelineSeqDtos() != null) {
            for (SchedulingAuditPipelineSeqDto auditPipelineSeqDto : auditPipelineDto.getAuditPipelineSeqDtos()) {
                auditPipelineSeqDto.setAuditPipelineId(auditPipelineDto.getId());
                //保存序列
                Long seqId = save(auditPipelineSeqDto);

                if (SchedulingAuditPipelineSeqConstants.AUDIT_TYPE_DEPARTMENT_DESIGNATED_PERSON.equals(auditPipelineSeqDto.getAuditType())) {
                    //如果任何人是具体人员需保存集体人员到关系表
                    List<SchedulingAuditPipelineSeqDeptPersonDto> personDtoList = auditPipelineSeqDto.getAuditPipelineSeqDeptPersonDtos();
                    if (personDtoList != null) {
                        for (SchedulingAuditPipelineSeqDeptPersonDto personDto : personDtoList) {
                            personDto.setAuditPipelineSeqId(seqId);
                            schedulingAuditPipelineSeqDeptPersonService.save(personDto);
                        }
                    }
                }
            }
        }
    }

    @Override
    public List<SchedulingAuditPipelineSeqDto> selectByPipelineId(Long auditPipelineId) {
        List<SchedulingAuditPipelineSeqEntity> auditPipelineSeqEntities =
                schedulingAuditPipelineSeqDao.selectList(new LambdaQueryWrapper<SchedulingAuditPipelineSeqEntity>().eq(SchedulingAuditPipelineSeqEntity::getAuditPipelineId, auditPipelineId));
        return CopyUtils.copyList(auditPipelineSeqEntities, SchedulingAuditPipelineSeqDto.class);
    }
    


    @Override
    public SchedulingAuditPipelineSeqDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_AUDIT_PIPELINE_SEQ_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingAuditPipelineSeqDto selectById(Long id){
            SchedulingAuditPipelineSeqEntity entity = schedulingAuditPipelineSeqDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,SchedulingAuditPipelineSeqDto.class));
    }

    @Override
    public List<SchedulingAuditPipelineSeqDto> queryList(Map<String,Object> params){
        List<SchedulingAuditPipelineSeqEntity> list = schedulingAuditPipelineSeqDao.queryList(params);
        List<SchedulingAuditPipelineSeqDto> result =  CopyUtils.copyList(list,SchedulingAuditPipelineSeqDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingAuditPipelineSeqDto> queryListWithoutPadding(Map<String,Object> params){
        List<SchedulingAuditPipelineSeqEntity> list = schedulingAuditPipelineSeqDao.queryList(params);
        return CopyUtils.copyList(list,SchedulingAuditPipelineSeqDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingAuditPipelineSeqDto paddingField(SchedulingAuditPipelineSeqDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return schedulingAuditPipelineSeqDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingAuditPipelineSeqDto dto){
        SchedulingAuditPipelineSeqEntity entity = CopyUtils.copyObj(dto,SchedulingAuditPipelineSeqEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingAuditPipelineSeqDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingAuditPipelineSeqDto dto){
        SchedulingAuditPipelineSeqEntity entity = CopyUtils.copyObj(dto,SchedulingAuditPipelineSeqEntity.class);

        schedulingAuditPipelineSeqDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingAuditPipelineSeqDto dto) {
       SchedulingAuditPipelineSeqEntity entity = CopyUtils.copyObj(dto,SchedulingAuditPipelineSeqEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, schedulingAuditPipelineSeqDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingAuditPipelineSeqDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
